var Advertisements = (function Advertisements() {
	
	var STATUS = {
		ROBOCZE: 1,
		OPUBLIKOWANE: 2
	};
	
	return {
        createTable: function (data, isLoggedUserAdmin) {
	        var adverts = $.parseJSON(data);
	        var table = '<table class="table table-bordered">';
	        table += '<thead><tr><th>Tytuł</th><th>Treść</th><th>Autor</th><th>Status publikacji</th><th>Operacje</th></tr></thead>';
	        table += '<tbody>';
	        var i = 0;
	        for (i; i < adverts.length; i++) {
	            var advert = adverts[i].Advertisement;
	            var user = adverts[i].User;
	            var status = adverts[i].Status;
	            table += "<tr>";
	            table += "<td>" + advert.title + "</td>";
	            table += "<td>" + advert.content + "</td>";
	            table += "<td>" + user.username + "</td>";
	            
	            var advStatus = "";
	            if (isLoggedUserAdmin === 1) {
	            	if (status.id == STATUS.ROBOCZE) {
	            		advStatus = "<center><a href='/advertisements/publish/" + advert.id + "'>" +
	            			"<img src='img/book_add.png' />" +"<br/>Publikuj</a></center>";
	            	} else {
	            		advStatus = "<center><a href='/advertisements/unpublish/" + advert.id + "'>" +
	            			"<img src='img/book_delete.png' />" +"<br/>Anuluj publikację</a></center>";
	            	}
	            } else {
	            	advStatus = status.status;
	            }
	            
	            table += "<td>" + advStatus + "</td>";
	            if (status.id == STATUS.ROBOCZE) {
		            var edit = "<a  style='float:left;' href='/advertisements/edit/"+ advert.id +"'>" +
		            	"<img src='img/page_white_edit.png' />" +"<br/>Edytuj</a>";
		            
		            var _delete = "<a style='float:left; padding-left:10px' href='/advertisements/delete/"+ advert.id +"'>" +
		            	"<img src='img/page_white_delete.png' />" +"<br/>Usuń</a>";
		            
	            	table += "<td><center>" + edit  +   _delete + "</center></td>";
	            } else {
	            	table += "<td> - </td>";
	            }
	            table += "</td>";
	            table += "</tr>";
	        }
	        table += '</tbody></table>';
	        $('#advertisementsTable').append(table);
    	}
	};
})();